package com.jsyxdl.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.PageInfo;
import com.hustcad.plm.pdm.common.model.vo.search.ResultEntityVO;
import com.hustcad.plm.pdm.folder.model.dto.filter.TyplmFolderObjDTO;
import com.hustcad.plm.pdm.folder.service.impl.search.TyplmSubFilterObjectExtendServiceImpl;
import com.hustcad.plm.pdm.mpmlink.model.dto.TyplmMpmCombinNameDTO;
import com.hustcad.plm.pdm.mpmlink.service.TyplmMpmCommonAdaptationService;
import com.hustcad.plm.pdm.partbom.service.TyplmPartService;
import com.hustcad.plm.pdm.partbom.vo.PartDetailsVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Primary
public class ExtendTyplmSubFilterObjectExtendServiceImpl extends TyplmSubFilterObjectExtendServiceImpl {
    private static final Logger log = LoggerFactory.getLogger(ExtendTyplmSubFilterObjectExtendServiceImpl.class);
    @Resource
    private TyplmMpmCommonAdaptationService typlmMpmCommonAdaptationService;
    @Resource
    private TyplmPartService typlmPartService;


    @Override
    public void excuteAfterSubFilterObject(PageInfo<TyplmFolderObjDTO> pageInfo){
        super.excuteAfterSubFilterObject(pageInfo);
        try {
            List<String> otypeList = new ArrayList();
            otypeList.add("ty.inteplm.mpm.CTyMPMOperation");
            otypeList.add("ty.inteplm.mpm.CTyMPMProcessPlan");
            otypeList.add("ty.inteplm.mpm.CTyMPMPlant");
            otypeList.add("ty.inteplm.mpm.CTyMPMWorkCenter");
            otypeList.add("ty.inteplm.mpm.CTyMPMSkill");
            otypeList.add("ty.inteplm.mpm.CTyMPMProcessMaterial");
            otypeList.add("ty.inteplm.mpm.CTyMPMTooling");
            otypeList.add("ty.inteplm.part.CTyPart");

            List<TyplmFolderObjDTO> list = pageInfo.getList();
            List<TyplmMpmCombinNameDTO> combinNameDTOList = new ArrayList();
            if (CollUtil.isNotEmpty(list)) {
                list.stream().filter((typlmFolderObjDTO) -> {
                    return otypeList.contains(typlmFolderObjDTO.getOtype());
                }).forEach((typlmFolderObj) -> {
                    TyplmMpmCombinNameDTO typlmMpmCombinName = new TyplmMpmCombinNameDTO();
                    typlmMpmCombinName.setOid(typlmFolderObj.getOid());
                    typlmMpmCombinName.setOtype(typlmFolderObj.getOtype());
                    combinNameDTOList.add(typlmMpmCombinName);
                });

                List<TyplmMpmCombinNameDTO> mpmObjCombine = this.typlmMpmCommonAdaptationService.getMpmObjCombine(combinNameDTOList);

                Map<BigInteger, String> collect=new HashMap<>();
                for(int i=0;i<mpmObjCombine.size();i++){
                    TyplmMpmCombinNameDTO typlmMpmCombinNameDTO=mpmObjCombine.get(i);
                    collect.put(typlmMpmCombinNameDTO.getOid(),typlmMpmCombinNameDTO.getCombinName());
                }
                for(int i=0;i<list.size();i++){
                    TyplmFolderObjDTO typlmFolderObjDTO=list.get(i);
                    if("ty.inteplm.part.CTyPart".equals(typlmFolderObjDTO.getOtype())){
                        try{
                            PartDetailsVO partDetailsVO = typlmPartService.queryPartDetails(typlmFolderObjDTO.getOid(), typlmFolderObjDTO.getOtype());
                            Map<String, List<String>> map=partDetailsVO.getIbaAttributeMap();
                            if(map.containsKey("规格型号")){
                                List<String> strs=map.get("规格型号");
                                typlmFolderObjDTO.setIdentifier(String.format("%s,%s,%s.%s,%s(%s)",typlmFolderObjDTO.getNumber(),strs.get(0),typlmFolderObjDTO.getName(),
                                                                              typlmFolderObjDTO.getVersion(),typlmFolderObjDTO.getIterationid(),
                                                                              typlmFolderObjDTO.getView()));
                            }
                        }
                        catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                }
            }
        } catch (Exception var7) {
            log.error("获取工艺对象标识出错，{}", var7);
        }
    }

    public void excuteAfterSubFilterObjectClass(PageInfo<ResultEntityVO> pageInfo){
        try {
            List<String> otypeList = new ArrayList();
            otypeList.add("ty.inteplm.mpm.CTyMPMOperation");
            otypeList.add("ty.inteplm.mpm.CTyMPMProcessPlan");
            otypeList.add("ty.inteplm.mpm.CTyMPMPlant");
            otypeList.add("ty.inteplm.mpm.CTyMPMWorkCenter");
            otypeList.add("ty.inteplm.mpm.CTyMPMSkill");
            otypeList.add("ty.inteplm.mpm.CTyMPMProcessMaterial");
            otypeList.add("ty.inteplm.mpm.CTyMPMTooling");
            otypeList.add("ty.inteplm.part.CTyPart");

            List<ResultEntityVO> list = pageInfo.getList();
            List<TyplmMpmCombinNameDTO> combinNameDTOList = new ArrayList();
            if (CollUtil.isNotEmpty(list)) {
                list.stream().filter((typlmFolderObjDTO) -> {
                    return otypeList.contains(typlmFolderObjDTO.getOtype());
                }).forEach((typlmFolderObj) -> {
                    TyplmMpmCombinNameDTO typlmMpmCombinName = new TyplmMpmCombinNameDTO();
                    typlmMpmCombinName.setOid(typlmFolderObj.getOid());
                    typlmMpmCombinName.setOtype(typlmFolderObj.getOtype());
                    combinNameDTOList.add(typlmMpmCombinName);
                });

                List<TyplmMpmCombinNameDTO> mpmObjCombine = this.typlmMpmCommonAdaptationService.getMpmObjCombine(combinNameDTOList);

                Map<BigInteger, String> collect=new HashMap<>();
                for(int i=0;i<mpmObjCombine.size();i++){
                    TyplmMpmCombinNameDTO typlmMpmCombinNameDTO=mpmObjCombine.get(i);
                    collect.put(typlmMpmCombinNameDTO.getOid(),typlmMpmCombinNameDTO.getCombinName());
                }
                for(int i=0;i<list.size();i++){
                    ResultEntityVO typlmFolderObjDTO=list.get(i);
                    if("ty.inteplm.part.CTyPart".equals(typlmFolderObjDTO.getOtype())){
                        try{
                            PartDetailsVO partDetailsVO = typlmPartService.queryPartDetails(typlmFolderObjDTO.getOid(), typlmFolderObjDTO.getOtype());
                            Map<String, List<String>> map=partDetailsVO.getIbaAttributeMap();
                            if(map.containsKey("规格型号")){
                                List<String> strs=map.get("规格型号");
                                typlmFolderObjDTO.setName(String.format("%s,%s.%s,%s(%s)",strs.get(0),typlmFolderObjDTO.getName(),
                                                                              typlmFolderObjDTO.getVersion(),typlmFolderObjDTO.getIterationid(),
                                                                              typlmFolderObjDTO.getView()));
                            }
                        }
                        catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                }
            }
        } catch (Exception var7) {
            log.error("获取工艺对象标识出错，{}", var7);
        }
    }

}
